package org.rency.ues.dal.repository.impl;

import org.apache.commons.lang.StringUtils;
import org.rency.ues.dal.dao.DataHashDao;
import org.rency.ues.dal.entity.DataHashDO;
import org.rency.ues.dal.repository.DataHashRepository;

public class DataHashRepositoryImpl implements DataHashRepository{
	
	private DataHashDao dataHashDao;

	@Override
	public boolean save(DataHashDO entity) {
		return dataHashDao.insert(entity) == 1;
	}

	@Override
	public boolean delete(int id) {
		return dataHashDao.delete(id) == 1;
	}

	@Override
	public boolean deleteByTicket(String ticket) {
		return dataHashDao.deleteByTicket(ticket) == 1;
	}

	@Override
	public boolean deleteByHash(String hash) {
		return dataHashDao.deleteByHash(hash) == 1;
	}

	@Override
	public DataHashDO get(int id) {
		return dataHashDao.get(id);
	}

	@Override
	public DataHashDO getByTicket(String ticket) {
		return dataHashDao.getByTicket(ticket);
	}

	@Override
	public DataHashDO getByHash(String hash) {
		return dataHashDao.getByHash(hash);
	}

	public void setDataHashDao(DataHashDao dataHashDao) {
		this.dataHashDao = dataHashDao;
	}

	@Override
	public DataHashDO exists(String ticket, String hash) {
		if( StringUtils.isBlank(ticket) && StringUtils.isBlank(hash)){
			throw new IllegalArgumentException("ticket or hash has value at least.");
		}
		if(StringUtils.isNotBlank(ticket)){
			return this.getByTicket(ticket);
		}
		if(StringUtils.isNotBlank(hash)){
			return this.getByHash(hash);
		}
		return null;
	}


}
